package com.loop.unclelend.utils.dialog

import android.Manifest
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.loop.unclelend.R
import com.loop.unclelend.databinding.UncleDialogPhotoBinding
import com.loop.unclelend.pro.ProDialog
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType
import com.loop.unclelend.utils.permission.OnPermissionCallback
import com.loop.unclelend.utils.permission.XXPermissions

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/12 17:59
 * @Annotation：拍照、相册选择 dialog
 */
class Dialog_Photo: ProDialog {
    private lateinit var binding: UncleDialogPhotoBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    override fun initWindow() {
        val params = window!!.attributes
        params.width =  WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window!!.setGravity(Gravity.BOTTOM)
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = UncleDialogPhotoBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(binding.imgClose)//关闭
        //拍照
        binding.layoutCamera.setOnClickListener {
            showUncleCameraClick()
        }
        //相册选择
        binding.layoutAlbum.setOnClickListener {
            showUncleAlbumClick()
        }
    }

    override fun initData() {

    }

    override fun onViewClick(v: View?) {
        dismiss()
    }

    /**
     * 拍照
     */
    private fun showUncleCameraClick(){
        val permissions = arrayListOf(
            Manifest.permission.CAMERA
        )
        if (XXPermissions.isGranted(context,permissions)){
            if(mCallback != null){
                dismiss()
                mCallback!!.openCamera()
            }
        } else {
            XXPermissions.with(context)
                // 申请多个权限
                .permission(permissions)
                .request(object: OnPermissionCallback {
                    override fun onGranted(p0: MutableList<String>, p1: Boolean) {
                        var permissionString = ""
                        p0.forEach {
                            permissionString = "$permissionString$it,"
                        }
                        UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_PERMISSION_SYSTEM_AGREE,permissionString.substring(0,permissionString.length - 1))
                        if(mCallback != null){
                            dismiss()
                            mCallback!!.openCamera()
                        }
                    }
                    override fun onDenied(p0: MutableList<String>, doNotAskAgain: Boolean) {
                        var permissionString = ""
                        p0.forEach {
                            permissionString = "$permissionString$it,"
                        }
                        UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_PERMISSION_SYSTEM_DENY,permissionString.substring(0,permissionString.length - 1))
                        XXPermissions.startPermissionActivity(context,permissions)
                    }
                })
        }
    }

    /**
     * 相册
     */
    private fun showUncleAlbumClick(){
        if(mCallback != null){
            dismiss()
            mCallback!!.openAlbum()
        }
    }

    interface IDialogCallback {
        //打开相机
        fun openCamera()
        //打开相册
        fun openAlbum()
    }

    override fun onStart() {
        super.onStart()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.DIALOG.UNCLE_DIALOG_PHOTO_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.DIALOG.UNCLE_DIALOG_PHOTO_DISMISS,eventRemark)
    }
}